/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class TiendaDAOImpl implements orm.dao.TiendaDAO {
	public Tienda loadTiendaByORMID(Integer idTienda) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadTiendaByORMID(session, idTienda);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda getTiendaByORMID(Integer idTienda) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return getTiendaByORMID(session, idTienda);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByORMID(Integer idTienda, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadTiendaByORMID(session, idTienda, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda getTiendaByORMID(Integer idTienda, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return getTiendaByORMID(session, idTienda, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByORMID(PersistentSession session, Integer idTienda) throws PersistentException {
		try {
			return (Tienda) session.load(orm.Tienda.class, idTienda);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda getTiendaByORMID(PersistentSession session, Integer idTienda) throws PersistentException {
		try {
			return (Tienda) session.get(orm.Tienda.class, idTienda);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByORMID(PersistentSession session, Integer idTienda, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Tienda) session.load(orm.Tienda.class, idTienda, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda getTiendaByORMID(PersistentSession session, Integer idTienda, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Tienda) session.get(orm.Tienda.class, idTienda, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda[] listTiendaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return listTiendaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda[] listTiendaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return listTiendaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda[] listTiendaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tienda as Tienda");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Tienda[]) list.toArray(new Tienda[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda[] listTiendaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tienda as Tienda");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Tienda[]) list.toArray(new Tienda[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadTiendaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return loadTiendaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Tienda[] tiendas = listTiendaByQuery(session, condition, orderBy);
		if (tiendas != null && tiendas.length > 0)
			return tiendas[0];
		else
			return null;
	}
	
	public Tienda loadTiendaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Tienda[] tiendas = listTiendaByQuery(session, condition, orderBy, lockMode);
		if (tiendas != null && tiendas.length > 0)
			return tiendas[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateTiendaByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return iterateTiendaByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTiendaByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.MarketFrogPersistentManager.instance().getSession();
			return iterateTiendaByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTiendaByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tienda as Tienda");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateTiendaByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Tienda as Tienda");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda createTienda() {
		return new orm.Tienda();
	}
	
	public boolean save(orm.Tienda tienda) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().saveObject(tienda);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Tienda tienda) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().deleteObject(tienda);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Tienda tienda)throws PersistentException {
		try {
			if(tienda.getEmpresaidEmpresa() != null) {
				tienda.getEmpresaidEmpresa().tienda.remove(tienda);
			}
			
			orm.Sucursal[] lSucursals = tienda.sucursal.toArray();
			for(int i = 0; i < lSucursals.length; i++) {
				lSucursals[i].setTiendaidTienda(null);
			}
			return delete(tienda);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Tienda tienda, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(tienda.getEmpresaidEmpresa() != null) {
				tienda.getEmpresaidEmpresa().tienda.remove(tienda);
			}
			
			orm.Sucursal[] lSucursals = tienda.sucursal.toArray();
			for(int i = 0; i < lSucursals.length; i++) {
				lSucursals[i].setTiendaidTienda(null);
			}
			try {
				session.delete(tienda);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Tienda tienda) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().getSession().refresh(tienda);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Tienda tienda) throws PersistentException {
		try {
			orm.MarketFrogPersistentManager.instance().getSession().evict(tienda);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Tienda loadTiendaByCriteria(TiendaCriteria tiendaCriteria) {
		Tienda[] tiendas = listTiendaByCriteria(tiendaCriteria);
		if(tiendas == null || tiendas.length == 0) {
			return null;
		}
		return tiendas[0];
	}
	
	public Tienda[] listTiendaByCriteria(TiendaCriteria tiendaCriteria) {
		return tiendaCriteria.listTienda();
	}
}
